﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DrugsShopManagement.DBCLASS;

namespace DrugsShopManagement
{
    public partial class FrmLogin : DevExpress.XtraEditors.XtraForm
    {
        public static string uid = "";
        public static string user_no = "";
        public static string user_name = "";
        public string user_power = "";
        Operator op = new Operator();
        public FrmLogin()
        {
            InitializeComponent();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (txtBoxUserID.Text.Trim() == "")
            {
                MessageBox.Show("请输入工号！");
                txtBoxUserID.Focus();
                return;
            }
            if (txtBoxPWD.Text.Trim() == "")
            {
                MessageBox.Show("请输入密码！");
                txtBoxPWD.Focus();
                return;
            }
            DataTable dt = new DataTable();
            dt = op.getDataTable("select * from users where user_no='" + txtBoxUserID.Text.Trim() + "'", "users");
            if (dt.Rows.Count <= 0)
            {
                MessageBox.Show("工号输入有误，请重新输入！");
                txtBoxUserID.Text = "";
                txtBoxUserID.Focus();
                return;
            }
            dt = op.getDataTable("select * from users where user_no='" + txtBoxUserID.Text.Trim() + "' and user_pwd='" + txtBoxPWD.Text.Trim() + "'", "");
            if (dt.Rows.Count <= 0)
            {
                MessageBox.Show("密码输入有误，请重新输入！");
                txtBoxPWD.Text = "";
                txtBoxPWD.Focus();
                return;
            }
            uid = dt.Rows[0]["user_no"].ToString().Trim();
            user_no = dt.Rows[0]["user_no"].ToString().Trim();
            user_name = dt.Rows[0]["user_name"].ToString().Trim();
            user_power = dt.Rows[0]["user_power"].ToString().Trim();
            FrmMain m = new FrmMain(user_no, user_name, user_power);
            //m.FormClosed += delegate { this.Close(); };
            m.Show();
            m.Text = m.Text.Trim() + "--登录用户：" + user_name;
            this.Visible = false;
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            this.txtBoxUserID.Focus();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.txtBoxPWD.Focus();
            }
        }

        private void textEdit2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.simpleButton1.Focus();
            }
        }

        private void FrmLogin_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void simpleButton1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.simpleButton1_Click(sender,e);
            }
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                this.simpleButton1_Click(sender, e);
            }
        }
    }
}
